package com.example.baseapp.controller;

import com.example.baseapp.common.ApiResponse;
import com.example.baseapp.dto.CategoryWithDishesDTO;
import com.example.baseapp.entity.Category;
import com.example.baseapp.entity.Dish;
import com.example.baseapp.service.CategoryService;
import com.example.baseapp.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/api/menu")
public class MenuController {
    
    @Autowired
    private CategoryService categoryService;
    
    @Autowired
    private DishService dishService;
    
    // 小程序端：获取完整菜单（分类+菜品）
    @GetMapping("/full")
    public ApiResponse<List<CategoryWithDishesDTO>> getFullMenu() {
        List<Category> categories = categoryService.getAllActiveCategories();
        List<CategoryWithDishesDTO> result = new ArrayList<>();
        
        for (Category category : categories) {
            List<Dish> dishes = dishService.getActiveDishesByCategory(category.getId());
            result.add(new CategoryWithDishesDTO(category, dishes));
        }
        
        return ApiResponse.success(result);
    }
    
    // 小程序端：获取所有分类
    @GetMapping("/categories")
    public ApiResponse<List<Category>> getCategories() {
        return ApiResponse.success(categoryService.getAllActiveCategories());
    }
    
    // 小程序端：根据分类获取菜品
    @GetMapping("/dishes/{categoryId}")
    public ApiResponse<List<Dish>> getDishesByCategory(@PathVariable Long categoryId) {
        return ApiResponse.success(dishService.getActiveDishesByCategory(categoryId));
    }
} 